import multiprocessing
from sqlalchemy import create_engine
from config import MYSQL_DB, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_PORT, MYSQL_USER
from config import Standardized_MYSQL_DB, Standardized_MYSQL_PASSWORD,Standardized_MYSQL_HOST, Standardized_MYSQL_PORT, \
                    Standardized_MYSQL_USER
from sqlalchemy.orm import scoped_session, sessionmaker


class CreateEngine(object):

    @property
    def _get_file_mysql_url(self):
        mysql_url = f'mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}' \
                    f':{MYSQL_PORT}/{MYSQL_DB}?' \
                    f'charset=utf8mb4'
        return mysql_url

    @property
    def _get_standardized_mysql_url(self):
        mysql_url = f'mysql+pymysql://{Standardized_MYSQL_USER}:{Standardized_MYSQL_PASSWORD}@{Standardized_MYSQL_HOST}' \
                    f':{Standardized_MYSQL_PORT}/{Standardized_MYSQL_DB}?' \
                    f'charset=utf8mb4'
        return mysql_url

    @property
    def get_mysql_url(self):
        return {
            "db_session": self._get_file_mysql_url,
            "standardized_session": self._get_standardized_mysql_url
        }

    def get_engine(self, method="db_session"):
        engine = create_engine(self.get_mysql_url[method], pool_size=multiprocessing.cpu_count() * 1,
                               pool_recycle=28800)
        return scoped_session(sessionmaker(bind=engine, autocommit=True))

